package com.lanou.connotationjoke.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.widget.TabHost;
import android.widget.Toast;

import com.lanou.connotationjoke.R;
import com.lanou.connotationjoke.fragment.DiscoveryFragment;
import com.lanou.connotationjoke.fragment.HomeFragment;
import com.lanou.connotationjoke.fragment.MsgFragment;
import com.lanou.connotationjoke.fragment.ReviewFragment;


public class MainActivity extends BaseActivity{

    private TabHost tabHost;
    private long exitTime = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tabHost = (TabHost) findViewById(android.R.id.tabhost);
        tabHost.setup();
        tabHost.addTab(tabHost.newTabSpec("tab_home")
                .setIndicator(getLayoutInflater().inflate(R.layout.tab_home, null))
                .setContent(R.id.tab_home));
        tabHost.addTab(tabHost.newTabSpec("tab_discovery")
                .setIndicator(getLayoutInflater().inflate(R.layout.tab_discovery, null))
                .setContent(R.id.tab_discovery));
        tabHost.addTab(tabHost.newTabSpec("tab_review")
                .setIndicator(getLayoutInflater().inflate(R.layout.tab_review, null))
                .setContent(R.id.tab_review));
        tabHost.addTab(tabHost.newTabSpec("tab_msg")
                .setIndicator(getLayoutInflater().inflate(R.layout.tab_msg, null))
                .setContent(R.id.tab_msg));
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.tab_home,new HomeFragment());
        transaction.replace(R.id.tab_discovery,new DiscoveryFragment());
        transaction.replace(R.id.tab_review,new ReviewFragment());
        transaction.replace(R.id.tab_msg, new MsgFragment());
        transaction.commit();

        //注册事件
//        EventBus.getDefault().register(this);


    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        //取消注册
//        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onBackPressed() {
        if (System.currentTimeMillis() - exitTime > 2000){
            exitTime = System.currentTimeMillis();
            Toast.makeText(MainActivity.this, "再按一次退出", Toast.LENGTH_SHORT).show();
        }else {
            finish();
        }
    }
    //根据post()中的参数选择重载方法

//
//
//    //在那个线程中发送的post()在那个线程中执行该回调方法
//    @Subscribe(threadMode = ThreadMode.PostThread)
//    public void onEvent(){
//
//    }
//
//    //不管事件在那个线程中发送的 都主线程中执行该方法
//    @Subscribe(threadMode = ThreadMode.MainThread)
//    public void onMainEvent(String s) {
////        Toast.makeText(MainActivity.this, "EventBus回调方法", Toast.LENGTH_SHORT).show();
////        imageView.setVisibility(View.GONE);
//    }
//
//    //肯定在子线程中回调
//    //主线程发送 新建子线程回调 子线程发送在当前子线程回调
//    @Subscribe(threadMode = ThreadMode.BackgroundThread)
//    public void onEventBackground(){
//
//    }
//
//    //不管在那个线程post()都会新建一个子线程回调该方法
//    @Subscribe(threadMode = ThreadMode.Async)
//    public void onEventAsync() {
//    }


}
